package codeTop;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class q139 {
    //超时
    public boolean wordBreak(String s, List<String> wordDict) {
        Set<String> set = new HashSet<>(wordDict);
        dfs(s,set,1,0);
        return flag;
    }
    boolean flag = false;
    void dfs(String s, Set<String> set,int idx,int pre){
        if (flag) {
            return;
        }
        if (idx == s.length()) {
            if (set.contains(s.substring(pre, idx))) {
                flag = true;
            }
            return;
        }
        //切分
        if (set.contains(s.substring(pre, idx))) {
            dfs(s,set,idx+1,idx);
        }
        //不切分
        dfs(s,set,idx+1,pre);
    }

    public boolean wordBreak1(String s, List<String> wordDict) {
        Set<String> set = new HashSet<>(wordDict);
        boolean[] dp = new boolean[s.length()+1];
        dp[0] = true;
        for (int i = 0; i < s.length(); i++) {
            for (int j = 0; j <= i; j++) {
                dp[i+1] = dp[i+1]||(dp[j]&&set.contains(s.substring(j,i+1)));
            }
        }
        return dp[s.length()];
    }

    public static void main(String[] args) {
        q139 q = new q139();
        q.wordBreak1("a",List.of(new String[]{"a"}));
    }
}
